<?php
// 本类由系统自动生成，仅供测试用途
namespace Home\Controller;
use Think\Controller;
use Home\Common\Page;
class IndexController extends Controller {
    public function index(){
        $news = D('News');
        $list = $news->where('type=1')->field('id,title,date,summary')->order('date desc')->select();
        foreach ($list as &$v) {
            $v['date'] = date('Y-m-d', $v['date']);
            $v['href'] = U('view',array('id'=>$v['id']));
        }
        $this->assign('list1', $list);

        $this->page();

        if ( isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest' ) {
            exit($this->fetch('./Public/indexajaxlist.html'));
        } else {
            $this->display('./Public/index.html');
        }
    }

    private function page() {
        $news = D('News');
        $result = $news->where('type=2')->field('id,title,pid')->order('id desc')->select();

        $p = new Page(count($result), 8, array());
        $page = $p->show();
        $this->assign('page', $page);

        $nowPage = empty($_GET['p']) ? 1 : intval($_GET['p']);
        $listRows = 8;
        $firstRow = ($nowPage - 1) * $listRows;
        $list = array_slice($result, $firstRow, $listRows);
        $resource = D('Resource');
        foreach ($list as &$v) {
            $r = $resource->where('id=%d', $v['pid'])->find();
            $v['path'] = $r['path'];
            $v['href'] = U('view',array('id'=>$v['id']));
        }
        $this->assign('list2_1', array_slice($list, 0, 4));
        count($list)>4 && $this->assign('list2_2', array_slice($list, 4, 4));
    }

    public function view($id) {
        $news = D('News');
        $result = $news->where("id=$id")->find();
//        $this->assign('title', $result['title']);
        $this->assign('content', htmlspecialchars_decode(stripslashes($result['content'])));
        $this->display('./Public/subview.html');
    }
}